package com.gsc.funnycode.ctrl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gsc.funnycode.entity.User;
import com.gsc.funnycode.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.util.Json;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.provider.MD5;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;


/**
 * @Author: Orange
 * @Date: 14:09 2022/4/30
 */
@Api("系统登录")
@Slf4j
@Controller
public class LoginController {

    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 去Login页面
     * @return
     */
    @GetMapping("/")
    public String toLogin(){
        return "Login";
    }
    @GetMapping("/toR")
    public String toRegister(){
        return "Register";
    }

    /**
     * 登录
     * @param name
     * @param pass
     * @param model
     * @return
     */
    @PostMapping("/Login")
    public String Login(String name, String pass, Model model, HttpServletRequest request){
        log.info("name:{}",name);
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.eq("account",name);
        wrapper.eq("password",SecureUtil.md5(pass));
        User user = userService.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(user)){
            //存Redis用于单点登录
            redisTemplate.opsForValue().set(user.getAccount(), JSONUtil.parse(user).toString(),1,TimeUnit.HOURS);
            //将key存入session
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            return "index/index";
        }
        model.addAttribute("msg","账号或密码错误");
        return "Login";
    }

    /**
     * 注册
     * @param name
     * @param pass
     * @param model
     * @return
     */
    @PostMapping("/Register")
    public String Register(String name, String pass,Model model,HttpServletRequest request){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("account",name);
        if (ObjectUtils.isNotEmpty(userService.list(wrapper))){
            model.addAttribute("msg","用户名已被注册！");
            return "Register";
        }
        User user = new User();
        user.setAccount(name);
        user.setPassword(SecureUtil.md5(pass));
        if (userService.save(user)){
            return Login(name,pass,model,request);
        }
        model.addAttribute("msg","注册失败！快去联系程序员");
        return "Register";
    }
}
